package tw.rescue.dao.impl.slim3;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.ModelQuery;

import tw.rescue.dao.TestModelDao;
import tw.rescue.slim3.meta.TestModelMeta;
import tw.rescue.slim3.model.TestModel;

import com.google.appengine.api.datastore.Key;

public class Slim3TestModelImpl implements TestModelDao {

	@Override
	public void newTestModel(TestModel model) {
		Key key = Datastore.allocateId(TestModel.class);
		model.setKey(key);
		Datastore.put(model);
	}

	@Override
	public List<TestModel> queryTestModel(String name) {
		TestModelMeta meta = TestModelMeta.get();
		ModelQuery<TestModel>q = Datastore.query(meta);
		if (!StringUtils.isBlank(name))
			q.filter(meta.name.equal(name));
		return q.sort(meta.name.desc).asList();
	}

}
